package com.miniserver.tql.impl;

import com.miniserver.tql.TqlPool;
import com.miniserver.tql.database.Configuration;
import com.miniserver.tql.database.impl.ConnectionPoolImpl;
import com.miniserver.util.LogUtil;

public class HsqlPool extends ConnectionPoolImpl implements TqlPool {
	public static final String CONNECT_TYPE_FILE = "file";
	public static final String CONNECT_TYPE_RES = "res";
	
	private String syncSqlFile;
	private String primaryKeyFile;
	private String dbFolder;
	private String connectType;

	public HsqlPool(String dbFolder, String name) {
		super(name);
		this.dbFolder = dbFolder;
		this.log = LogUtil.getLogUtil("HsqlPool");
		this.initConnectionPool(null);
	}
	
	public HsqlPool(String dbFolder, String name, String conntectType) {
		super(name);
		this.dbFolder = dbFolder;
		this.connectType = conntectType;
		this.log = LogUtil.getLogUtil("HsqlPool");
		this.initConnectionPool(null);
	}

	@Override
	public String getDriverClassName(Configuration config) {
		return "org.hsqldb.jdbcDriver";
	}

	@Override
	public String getUrl(Configuration config) {
		if(this.connectType.equals(CONNECT_TYPE_RES)) {
			return String.format("jdbc:hsqldb:res:../..%s/%s", this.dbFolder, this.name);
		} else {
			return String.format("jdbc:hsqldb:file:%s/%s", this.dbFolder, this.name);
		}
	}

	@Override
	public String getUserName(Configuration config) {
		return "SA";
	}

	@Override
	public String getPassword(Configuration config) {
		return "";
	}

	public String getSyncSqlFile() {
		return syncSqlFile;
	}

	public void setSyncSqlFile(String syncSqlFile) {
		this.syncSqlFile = syncSqlFile;
	}

	public String getPrimaryKeyFile() {
		return this.primaryKeyFile;
	}

	public void setPrimaryKeyFile(String primaryKeyFile) {
		this.primaryKeyFile = primaryKeyFile;
	}

	public String getDbFolder() {
		return this.dbFolder;
	}

	public void setDbFolder(String dbFolder) {
		this.dbFolder = dbFolder;
	}

	public String getConnectType() {
		return connectType;
	}

	public void setConnectType(String connectType) {
		this.connectType = connectType;
	}
}